package com.dx.mes.common.dao.impl;

import static com.dx.mes.common.bean.def.GenericDef.BOX_CNT_NOT_ENOUGT;
import static com.dx.mes.common.bean.def.GenericDef.EMPTY_CARRIER;
import static com.dx.mes.common.bean.def.GenericDef.EXIST_CARRIER;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_RET_WO;
import static com.dx.mes.common.bean.def.GenericDef.PROC_EMPTY;
import static com.dx.mes.common.bean.def.GenericDef.PROC_IN_PROCESS;
import static com.dx.mes.common.bean.def.GenericDef.SHT_BNK_FLG_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_MDL_ID_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_NX_PROC_ID_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_NX_TOOLG_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_OPE_ID_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_OPE_NO_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_OPE_VER_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_PATH_ID_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_PATH_VER_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_STAT_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SHT_WO_ID_MIX;
import static com.dx.mes.common.bean.def.GenericDef.SLOT_NO_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;
import static com.dx.mes.common.bean.def.GenericDef._SPACE0;
import static com.dx.mes.common.bean.def.GenericDef._YES;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.dx.common.table.ppt.Ret_box;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.core.dao.impl.GenericHibernateDao;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.table.rtnBean.BoxInfoBeen;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;

@Repository
public class RetBoxHibernateDao extends GenericHibernateDao<Ret_box, String> implements IRetBoxDao {
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWODao;
	
	public SimpleRtnBeen configBoxInfo(Ret_box box ,String flg) {
		SimpleRtnBeen simpleRtnBeen = new SimpleRtnBeen(_NORMAL);
		Boolean isMixLot = false;
		// Get prd list
		List<Ret_prd_info> gRet_prd_infoList = new ArrayList<Ret_prd_info>();
		gRet_prd_infoList = retPrdInfoDao.getShtListInfo(box.getBox_id());
		if (null == gRet_prd_infoList) {
			// EMPT BOX
			simpleRtnBeen.setObj(this.setBoxEmpt(box));
		} else {
			// Check count
			if(flg.equals("Y")){
				if (gRet_prd_infoList.size() != box.getPrd_qty()) {
					simpleRtnBeen.setMsgBuf("箱子 [" + box.getBox_id() + "]的玻璃数量["
							+ box.getPrd_qty() + "]与装入的玻璃数量["
							+ gRet_prd_infoList.size() + "]不符!");
					simpleRtnBeen.setlRc(BOX_CNT_NOT_ENOUGT);
					return simpleRtnBeen;
				}
			}

			Ret_wo ret_wo = retWODao.get(gRet_prd_infoList.get(0).getWo_id_fk());
			if(ret_wo == null){
				simpleRtnBeen.setMsgBuf("找不到箱子 [" + box.getBox_id() + "]的内部订单["
						+ gRet_prd_infoList.get(0).getWo_id_fk() + "]!");
				simpleRtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
				return simpleRtnBeen;
			}
			
			// Check mix
			for (int i = 1; i < gRet_prd_infoList.size(); i++) {
				// Check status
				if (!gRet_prd_infoList.get(0).getPrd_stat()
						.equals(gRet_prd_infoList.get(i).getPrd_stat())) {
					simpleRtnBeen.setMsgBuf("状态不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getPrd_stat() + "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getPrd_stat() + "]!");
					simpleRtnBeen.setlRc(SHT_STAT_MIX);
					return simpleRtnBeen;
				}

				// Check NX_PATH_ID
				if (!gRet_prd_infoList.get(0).getNx_path_id_fk()
						.equals(gRet_prd_infoList.get(i).getNx_path_id_fk())) {
					simpleRtnBeen.setMsgBuf("下一工艺路线不能混！["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getNx_path_id_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getNx_path_id_fk()
							+ "]!");
					simpleRtnBeen.setlRc(SHT_PATH_ID_MIX);
					return simpleRtnBeen;
				}

				// Check NX_PATH_VER_FK
				if (!gRet_prd_infoList.get(0).getNx_path_ver_fk()
						.equals(gRet_prd_infoList.get(i).getNx_path_ver_fk())) {
					simpleRtnBeen.setMsgBuf("下一工艺路线版本不能混！["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getNx_path_ver_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getNx_path_ver_fk()
							+ "]!");
					simpleRtnBeen.setlRc(SHT_PATH_VER_MIX);
					return simpleRtnBeen;
				}

				// Check NX_OPE_NO_FK
				if (!gRet_prd_infoList.get(0).getNx_ope_no_fk()
						.equals(gRet_prd_infoList.get(i).getNx_ope_no_fk())) {
					simpleRtnBeen
							.setMsgBuf("下一站点编号不能混！ ["
									+ gRet_prd_infoList.get(0).getPrd_seq_id()
									+ "]["
									+ gRet_prd_infoList.get(0)
											.getNx_ope_no_fk()
									+ "] vs ["
									+ gRet_prd_infoList.get(i).getPrd_seq_id()
									+ "]["
									+ gRet_prd_infoList.get(i)
											.getNx_ope_no_fk() + "]!");
					simpleRtnBeen.setlRc(SHT_OPE_NO_MIX);
					return simpleRtnBeen;
				}

				// Check NX_OPE_ID_FK
				if (!gRet_prd_infoList.get(0).getNx_ope_id_fk()
						.equals(gRet_prd_infoList.get(i).getNx_ope_id_fk())) {
					simpleRtnBeen
							.setMsgBuf("下一站点ID不能混！ ["
									+ gRet_prd_infoList.get(0).getPrd_seq_id()
									+ "]["
									+ gRet_prd_infoList.get(0)
											.getNx_ope_id_fk()
									+ "] vs ["
									+ gRet_prd_infoList.get(i).getPrd_seq_id()
									+ "]["
									+ gRet_prd_infoList.get(i)
											.getNx_ope_id_fk() + "]!");
					simpleRtnBeen.setlRc(SHT_OPE_ID_MIX);
					return simpleRtnBeen;
				}

				// Check NX_OPE_VER_FK
				if (!gRet_prd_infoList.get(0).getNx_ope_ver_fk()
						.equals(gRet_prd_infoList.get(i).getNx_ope_ver_fk())) {
					simpleRtnBeen.setMsgBuf("下一站点版本不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getNx_ope_ver_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getNx_ope_ver_fk()
							+ "]!");
					simpleRtnBeen.setlRc(SHT_OPE_VER_MIX);
					return simpleRtnBeen;
				}

				// Check NX_PROC_ID_FK
				if (!gRet_prd_infoList.get(0).getNx_proc_id_fk()
						.equals(gRet_prd_infoList.get(i).getNx_proc_id_fk())) {
					simpleRtnBeen.setMsgBuf("下一制程不能混！["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getNx_proc_id_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getNx_proc_id_fk()
							+ "]!");
					simpleRtnBeen.setlRc(SHT_NX_PROC_ID_MIX);
					return simpleRtnBeen;
				}

				// Check NX_TOOLG_ID_FK
				/*if (!gRet_prd_infoList.get(0).getNx_toolg_id_fk()
						.equals(gRet_prd_infoList.get(i).getNx_toolg_id_fk())) {
					simpleRtnBeen.setMsgBuf("下一设备组不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getNx_toolg_id_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getNx_toolg_id_fk()
							+ "]!");
					simpleRtnBeen.setlRc(SHT_NX_TOOLG_MIX);
					return simpleRtnBeen;
				}*/

				// Check WO_ID_FK
				if (!gRet_prd_infoList.get(0).getWo_id_fk()
						.equals(gRet_prd_infoList.get(i).getWo_id_fk())) {
					simpleRtnBeen.setMsgBuf("内部订单不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getWo_id_fk() + "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getWo_id_fk() + "]!");
					simpleRtnBeen.setlRc(SHT_WO_ID_MIX);
					return simpleRtnBeen;
				}

				// Check MDL_ID_FK
				if (!gRet_prd_infoList.get(0).getMdl_id_fk()
						.equals(gRet_prd_infoList.get(i).getMdl_id_fk())) {
					simpleRtnBeen.setMsgBuf("产品ID不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getMdl_id_fk()
							+ "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getMdl_id_fk() + "]!");
					simpleRtnBeen.setlRc(SHT_MDL_ID_MIX);
					return simpleRtnBeen;
				}

				// Check BNK_FLG
				if (!gRet_prd_infoList.get(0).getBnk_flg()
						.equals(gRet_prd_infoList.get(i).getBnk_flg())) {
					simpleRtnBeen.setMsgBuf("入库标示不能混！ ["
							+ gRet_prd_infoList.get(0).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(0).getBnk_flg() + "] vs ["
							+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ gRet_prd_infoList.get(i).getBnk_flg() + "]!");
					simpleRtnBeen.setlRc(SHT_BNK_FLG_MIX);
					return simpleRtnBeen;
				}

			} // End of mix check

			// Check slot
			simpleRtnBeen = slotCheck(gRet_prd_infoList);
			if (_NORMAL != simpleRtnBeen.getlRc()) {
				return simpleRtnBeen;
			}

			// set box info
			if (PROC_IN_PROCESS.equals(gRet_prd_infoList.get(0).getPrd_stat())) {
				box.setBox_stat(PROC_EMPTY);
			} else {
				box.setBox_stat(gRet_prd_infoList.get(0).getPrd_stat());
			}
			box.setReal_emp(EXIST_CARRIER);

			// Set prd grade TODO:是否可以混等OK/NG
//			if (retPrdInfoDao.getOkPrdCnt(box.getBox_id()) > 0) {
//				box.setPrd_grade_fk(_OK);
//			} else {
//				box.setPrd_grade_fk(_NG);
//			}

			// Path info
			box.setNx_path_id_fk(gRet_prd_infoList.get(0).getNx_path_id_fk());
			box.setNx_path_ver_fk(gRet_prd_infoList.get(0).getNx_path_ver_fk());
			box.setNx_ope_no_fk(gRet_prd_infoList.get(0).getNx_ope_no_fk());
			box.setNx_ope_id_fk(gRet_prd_infoList.get(0).getNx_ope_id_fk());
			box.setNx_ope_ver_fk(gRet_prd_infoList.get(0).getNx_ope_ver_fk());

			// Prod & tool
			box.setNx_proc_id_fk(gRet_prd_infoList.get(0).getNx_proc_id_fk());
			box.setMdl_id_fk(gRet_prd_infoList.get(0).getMdl_id_fk());
			box.setMtrl_prod_id_fk(gRet_prd_infoList.get(0)
					.getMtrl_prod_id_fk());

			box.setNx_toolg_id_fk(gRet_prd_infoList.get(0).getNx_toolg_id_fk());
			box.setNx_tool_id_fk(_SPACE0);
			//下一站设备运行模式
			box.setNx_tool_run_mode(gRet_prd_infoList.get(0).getNx_tool_run_mode());
			
			box.setWo_id_fk(gRet_prd_infoList.get(0).getWo_id_fk());
			box.setBatch_no_fk(gRet_prd_infoList.get(0).getBatch_no());
			box.setBnk_flg(gRet_prd_infoList.get(0).getBnk_flg());
			box.setValid_flg(_YES);
			box.setPrty(gRet_prd_infoList.get(0).getPrty());
			box.setPv_prty(gRet_prd_infoList.get(0).getPv_prty());
			box.setDest_shop(gRet_prd_infoList.get(0).getDest_shop());
			box.setSo_id_fk(gRet_prd_infoList.get(0).getSo_id_fk());
			box.setPrd_qty(gRet_prd_infoList.size());
			box.setChip_qty(gRet_prd_infoList.get(0).getPrd_qty() * box.getPrd_qty());//面板计数
			if (null != gRet_prd_infoList.get(0).getPrd_admin_flg()){
				box.setPrd_admin_flg(gRet_prd_infoList.get(0).getPrd_admin_flg().substring(0, 1)+"NNNNNNNNNNNNNNNNNNN");
			}else{
				box.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");
			}
			box.setGroup_id(gRet_prd_infoList.get(0).getGroup_id());//更新箱子的group id
			simpleRtnBeen.setObj(box);
			
			this.update(box);
		}

		return simpleRtnBeen;
	}
	
	public Ret_box setBoxEmpt(Ret_box box) {
		box.setBox_stat(PROC_EMPTY);
		box.setReal_emp(EMPTY_CARRIER);
		box.setGroup_id(_SPACE0);
		//box.setBox_use_cnt(box.getBox_use_cnt() + 1);
		box.setPrd_grade_fk(_SPACE0);
		box.setPrd_qty(0);
		box.setChip_qty(0);
		box.setMdl_id_fk(_SPACE0);
		box.setMtrl_prod_id_fk(_SPACE0);
		box.setNx_path_id_fk(_SPACE0);
		box.setNx_path_ver_fk(_SPACE0);
		box.setNx_ope_id_fk(_SPACE0);
		box.setNx_ope_no_fk(_SPACE0);
		box.setNx_ope_ver_fk(_SPACE0);
		box.setNx_proc_id_fk(_SPACE0);
		box.setNx_tool_id_fk(_SPACE0);
		box.setNx_toolg_id_fk(_SPACE0);
		box.setTool_id_fk(_SPACE0);
		box.setTool_port_id_fk(_SPACE0);
		box.setPv_prty(box.getPrty());
		box.setPrty(_SPACE0);
		box.setWo_id_fk(_SPACE0);
		box.setDest_shop(_SPACE0);
		box.setSo_id_fk(_SPACE0);
		box.setPallet_id_fk(_SPACE0);
		box.setBatch_no_fk(_SPACE0);
		box.setRsv_no_fk(_SPACE0);
		box.setDn_no_fk(_SPACE0);
		box.setCus_no(_SPACE0);
		box.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");
		return box;
	}
	
	public SimpleRtnBeen slotCheck(List<Ret_prd_info> gRet_prd_infoList) {
		SimpleRtnBeen simpleRtnBeen = new SimpleRtnBeen(_NORMAL);
		String slotNO1 = "";
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {
			slotNO1 = gRet_prd_infoList.get(i).getSlot_no();
			if (!(Integer.valueOf(slotNO1) > 0)) {
				simpleRtnBeen.setMsgBuf("产品 ["
						+ gRet_prd_infoList.get(i).getPrd_seq_id() + "]的子格位号 ["
						+ slotNO1 + "]不正确");
				simpleRtnBeen.setlRc(SLOT_NO_INVALIED);
				return simpleRtnBeen;
			}
		}
		return simpleRtnBeen;
	}

	@Override
	public Boolean isInWipDestShop(Ret_box box) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SimpleRtnBeen chkMtrlBoxAndSlot(Ret_box box, boolean chkSlot) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> queryRetBoxInfoBySql(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BoxInfoBeen retBox2BoxInfo(Ret_box ret_box) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyCondition(String box_id, String box_stat, String nx_toolg_id_fk,
			String wo_id, String lot_id_fk) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyConditionWH(String box_id, String box_stat, String wo_id,
			String wo_ids, String dest_shop, String bnk_flg, String cus_id, String bnk_flgs, String so_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyConditionWipWH(String box_id, String box_stat, String wo_id,
			String dest_shop, String bnk_flg, String nx_ope_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Ret_box setBoxYield(Ret_box box) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Ret_box setBoxYield(Ret_box box, List<Ret_prd_info> gRet_prd_infoList) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Ret_box> queryRetBoxBySql(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyConditionWHwithFAB(String box_id, String box_stat, String wo_id,
			String wo_ids, String dest_shop, String bnk_flg, String cus_id, String so_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyConditionWHwithSHOP(String box_id, String box_stat, String wo_id,
			String wo_ids, String dest_shop, String bnk_flg, String cus_id, String so_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BoxInfoBeen> retBox2BoxInfoByManyConditionWipWHwithFAB(String box_id, String box_stat, String wo_id,
			String dest_shop, String bankOut, String ope_id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPalletBoxCnt(String palletId) {
		// TODO Auto-generated method stub
		return 0;
	}
}
